import json
import time

import pandas
from pymongo import MongoClient

from llm.text_analysis import llm_request

# 定义连接字符串，包含用户名和密码
uri = "mongodb://mongoadmin:maojiawei7750@123.57.232.135:27017/admin?minPoolSize=2&readPreference=primary&ssl=false"

# 创建MongoDB客户端连接
client = MongoClient(uri)

# 选择你要使用的数据库
db = client['test']

collection = db['wechat_message']


def conclude_dialog(group_name):
    # 获取当前时间
    # 格式化为 yyyy-MM-dd
    date_str = time.strftime('%Y-%m-%d', time.localtime())
    datetime_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("日期格式：", date_str)

    query = {
        "group_name": group_name,
        "date": date_str
    }
    # 执行查询并获取结果
    results = collection.find(query)
    llm_result = llm_request(results)
    print(llm_result)

    dataset = json.loads(llm_result)
    df = pandas.DataFrame(dataset)
    df = df.rename(columns={"id": "序号", "question": "问题", "speaker": "发起人", "speak_time": "发起时间", "completed": "解决状态"})
    file_name = date_str + '-' + datetime_str + '.xlsx'
    df.to_excel(file_name, index=False)


conclude_dialog('联空Agent测试')
